package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
)

type Goods struct {
	GoodId          int     `pk:"ai"`
	GoodName        string  `form:"goodName"`
	GoodPrices      float64 `form:"goodPrices"`
	GoodStorage     int     `form:"goodStorage"`
	GoodDescription string  `form:"goodInfo"`
	GoodKindId      int     `form:"goodKindId"`
	GoodUrl         string
	GoodCount       int
	GoodStoreId     int
	GoodIsSell      int
	GoodRefPrices   float64 `form:"goodRefPrices"`
	GoodStoreName   string  `ignore:"true"`
}

func SelectAllFromGoodWherePrimaryKey(pk int) Goods {
	newOrm := orm.NewOrm()
	var goods Goods
	sql := QueryByEqualField(Goods{}, "GoodId")
	e := newOrm.Raw(sql, pk).QueryRow(&goods)
	if e != nil {
		panic(e)
	}
	return goods
}

func SelectAllSellGood() []Goods {
	newOrm := orm.NewOrm()
	var goods []Goods
	sql := QueryByEqualField(Goods{}, "GoodIsSell")
	fmt.Println("sql", sql)
	_, e := newOrm.Raw(sql, 1).QueryRows(&goods)
	if e != nil {
		panic(e)
	}
	for i, v := range goods {
		goods[i].GoodStoreName = GetStoreByPrimarkKey(v.GoodStoreId).StoresName
	}
	return goods
}

func SelectAllGoodByName(name string) []Goods {
	newOrm := orm.NewOrm()
	var goods []Goods
	sql := "SELECT * FROM goods WHERE good_name like ? and good_is_sell = 1"
	name = "%" + name + "%"
	_, e := newOrm.Raw(sql, name).QueryRows(&goods)
	if e != nil {
		panic(e)
	}
	return goods
}

func SeletAllSellGoodByKindId(kindId, storeId int) []Goods {
	newOrm := orm.NewOrm()
	var goods []Goods
	sql := QueryByEqualField(Goods{}, "GoodKindId", "GoodStoreId", "GoodIsSell")
	_, e := newOrm.Raw(sql, kindId, storeId, 1).QueryRows(&goods)
	if e != nil {
		panic(e)
	}
	for i, v := range goods {
		goods[i].GoodStoreName = GetStoreByPrimarkKey(v.GoodStoreId).StoresName
	}
	return goods
}
